{extend name="common@admin/main" /}
{block name="content"}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary"> <i class="fa fa-save"></i>
                </button>
                <a href="" data-toggle="tooltip" title="取消" class="btn btn-default"> <i class="fa fa-reply"></i>
                </a>
            </div>
            <h1>管理员</h1>
            <ul class="breadcrumb">
                <li>
                    <a href="">首页</a>
                </li>
                <li>
                    <a href="">管理员</a>
                </li>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <i class="fa fa-pencil"></i>
                    设置管理员
                </h3>
            </div>
            <div class="panel-body">
                <!-- __ACTION__ -->
                <form action="{:url()}" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal">

                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#tab-general" data-toggle="tab">基本信息</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab-general">

                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-username">用户名</label>
                                <div class="col-sm-10">
                                    <input type="text" name="username" value="" placeholder="用户名" id="input-username" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-password">密码</label>
                                <div class="col-sm-10">
                                    <input type="text" name="password" value="" placeholder="密码" id="input-password" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-password_salt">盐</label>
                                <div class="col-sm-10">
                                    <input type="text" name="password_salt" value="" placeholder="盐" id="input-password_salt" class="form-control">

                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="input-password_salt">角色</label>
                                <div class="col-sm-10">
                                    <div class="row">

                                    </div>
                                </div>
                            </div>


                        </div>

                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="title"}
<title>角色授权</title>
{/block}

{block name="appendJS"}
<script>
    $(function(){
        var user={$user},
            role={$role},
            allrole={$allrole},
            back_user={$session_user};
            console.log(user)    
            console.log(role)    
            console.log(allrole)
            console.log(back_user)
             //角色列表
             var div=$('<div/>',{class:"col-sm-2"});
             $.each(allrole,function(key,val){
                  var input=$('<input/>',{
                       type:"checkbox",
                       name:val,
                       value:val.id,
                       class:"form-control", 
                       id:"input-role-"+val.id});
                  var label=$('<label/>',{for:"input-role-"+val.id}).html(val.name);
                  div.append(input).append(label);
                });
               $('.row').append(div);
               
            if(user!=null&&role!=null){
                //回显用户信息与所属角色信息
                $(':text[name="username"]').val(user.username).attr("disabled","false");;
                $(':text[name="password"]').val(user.password).attr("disabled","false");;
                $(':text[name="password_salt"]').val(user.password_salt).attr("disabled","false");;
        
                $.each(allrole,function(key,val){
                  var input=$(':checkbox[class:"form-control"]')
                  if(role!=null&&role.id==val.id){
                    input.prop('checked',true);
                  }  
                });
            }
        
            //提交修改
            var submit=$('.fa-save');
            submit.click(function(){
               var user_id=user.id;
               var role_id=$(':checkbox:checked').val();
               $.post('/admin/updateRole.json',{user_id:user_id,role_id:role_id},function(res){
                     if(res.code==1){
                        //跳转用户列表
                        location.href="/admin/permiss/userlist";
                     }else{
                        alert('修改失败');
                     }

               });
               return false;
            })
    }) 
</script>
{/block}
